package com.example.student.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;


import java.io.IOException;

/**
 * @author TonySong
 * @date 2025/8/7 0007
 * @time 19:48
 */
@WebFilter("/*")
@Slf4j
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("=====================进行字符编码过滤================");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                  HttpServletRequest request= (HttpServletRequest) servletRequest;
                  HttpServletResponse response= (HttpServletResponse) servletResponse;
                  request.setCharacterEncoding("utf-8");
                  response.setContentType("text/html;charset=utf-8");
                  response.setCharacterEncoding("utf-8");
                  filterChain.doFilter(request,response);
                  log.info("---》执行字符编码完毕《----");
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
